package com.xc.sys.service;

import com.xc.common.object.PageInfo;
import com.xc.db.tables.SysLoginLog;
import com.xc.db.tables.records.SysLoginLogRecord;
import com.xc.sys.entity.SysLoginLogEntity;
import java.util.List;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectField;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysLoginLogService {
	@Autowired
	private DSLContext dsl;
	private SysLoginLog LOGINLOG = SysLoginLog.SYS_LOGIN_LOG.as("LOGINLOG");

	public SysLoginLogEntity getEntityById(Integer id) {
		Record record = this.dsl.select(new SelectField[0]).from(this.LOGINLOG)
				.where(new Condition[] { LOGINLOG.ID.eq(id) }).fetchOne();
		SysLoginLogEntity user = null;
		if (record != null) {
			user = (SysLoginLogEntity) record.into(SysLoginLogEntity.class);
		}
		return user;
	}

	public Result<Record> select(Condition whr, PageInfo<SysLoginLogEntity> pageInfo,
			SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LOGINLOG).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
		}
		List<SysLoginLogEntity> list = result.into(SysLoginLogEntity.class);
		pageInfo.setResult(result);
		pageInfo.setList(list);
		return result;
	}

	public List<SysLoginLogEntity> select(Condition whr, SortField<?>... orderby) {
		Result<Record> result = null;
		if (orderby == null) {
			result = this.dsl.select(new SelectField[0]).from(this.LOGINLOG).where(whr).fetch();
		} else {
			result = this.dsl.select(new SelectField[0]).from(this.LOGINLOG).where(whr).orderBy(orderby).fetch();
		}
		List<SysLoginLogEntity> list = result.into(SysLoginLogEntity.class);
		return list;
	}

	public Long count(Condition cond) {
		Long count = null;
		count = (Long) this.dsl.selectCount().from(this.LOGINLOG).where(new Condition[] { cond }).fetchOneInto(Long.class);
		return count;
	}

	public void update(SysLoginLogEntity entity) {
		SysLoginLogRecord record = new SysLoginLogRecord();
		record.from(entity);
		dsl.executeUpdate(record);
	}

	public void update(Record record) {
		SysLoginLogRecord sysLoginLogRecord = new SysLoginLogRecord();
		sysLoginLogRecord.from(record);
		dsl.executeUpdate(sysLoginLogRecord);
	}

	public Integer insert(SysLoginLogEntity entity) {
		SysLoginLogRecord record = new SysLoginLogRecord();
		record.from(entity);
		Integer id = dsl.insertInto(LOGINLOG).set(record).returning(LOGINLOG.ID).fetchOne().getId();
		return id;
	}

	public void deleteById(Integer id) {
		dsl.delete(this.LOGINLOG).where(new Condition[] { this.LOGINLOG.ID.eq(id) }).execute();
	}

}
